/**
 * @ Author 何涛
 * @ Date 2016/02/19 12:57:21
 */

tyNamespace.ModelLayer = cc.LayerColor.extend({
    listener: null,
    fixedPriority: 0,
    touchArea: null,
    isTouchLimit: false,

    ctor: function (opacity) {
        this._super();

        opacity = opacity || 70;
        this.init(opacity);
    },

    init: function (opacity) {
        this.fixedPriority = -128;
        this.setColor(cc.color.BLACK);
        this.setOpacity(opacity);
    },

    setTouchArea: function (size, position) {
        this.touchArea = cc.rect(position.x, position.y, size.width, size.height);
        this.isTouchLimit = true;

        ////TODO 测试按钮触摸区域
        //var dn = new cc.DrawNode();
        //dn.drawRect(cc.p(position.x, position.y), cc.p(position.x + size.width, position.y + size.height), cc.color(0, 0, 0, 70), 1, cc.color(0, 0, 0, 70));
        //var rs = cc.director.getRunningScene();
        //rs.addChild(dn, 101);
        //rs.scheduleOnce(function () {
        //    dn.removeFromParent(true);
        //}, 5);
    },

    onEnter: function () {
        this._super();

        var self = this;

        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {
                var target = event.getCurrentTarget();
                var point = touch.getLocation();
                if (target.isTouchLimit) {
                    if (!cc.rectContainsPoint(self.touchArea, point))
                        return false;
                }
                return true;
            }
        });
        cc.eventManager.addListener(listener, this);
        this.listener = listener;
    }
});

tyNamespace.ModelLayerCancelable = cc.LayerColor.extend({
    listener: null,
    fixedPriority: 0,

    ctor: function (opacity) {
        this._super();

        opacity = opacity || 70;
        this.init(opacity);
    },

    init: function (opacity) {
        this.fixedPriority = -128;
        this.setColor(cc.color.BLACK);
        this.setOpacity(opacity);
    },

    onEnter: function () {
        this._super();

        var self = this;

        var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function () {
                return true;
            },
            onTouchEnded: function (touch, event) {
                var target = event.getCurrentTarget();
                target.removeFromParent(true);
                cc.eventManager.removeListener(self.listener);
            }
        });
        cc.eventManager.addListener(listener, this);
        this.listener = listener;
    }
});